package com.supeng.PMdepot.common;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;

import com.supeng.PMdepot.MApplication;
import com.supeng.PMdepot.entity.bak.been.User;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by Supeng on 2018/8/16.
 */

public class Config {
    /**
     * SharedPreferences 用户信息存储名字前缀
     */
    private static String USER_INFO = "USER_INFO";
    /**
     * 屏幕长宽参数
     */
    private static int width;
    private static int height;

    public static int getScreenWidth() {
        return width;
    }

    public static int getScreenHeight() {
        return height;
    }

    /**
     * 得到屏幕长宽
     *
     * @param activity
     */
    public static void setScreenSize(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = activity.getWindowManager().getDefaultDisplay();
        display.getMetrics(displayMetrics);
        width = displayMetrics.widthPixels;
        height = displayMetrics.heightPixels;
        if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
            try {
                height = (Integer) display.getClass().getMethod("getRawHeight").invoke(display);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (height == 0) {
            height = displayMetrics.heightPixels;
        }
    }

    public static SharedPreferences UserInfoPreferences = MApplication.getContext().getSharedPreferences(USER_INFO, 0);

    private static String USER = "User";
    public static User mUser = new User();

    /**
     * 设置用户信息
     *
     * @author
     * @since 2015-11-19 下午04:57:36
     */
    public static void setUserInfo(User user) {
        try {
            String userInfoStr = serialize(user);
            UserInfoPreferences.edit().putString(USER, userInfoStr).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mUser = user;
    }

    /**
     * 获取用户信息
     *
     * @author
     * @since 2015-11-19 下午04:57:36
     */
    public static User getUserInfo() {
        if (UserInfoPreferences.contains(USER)) {
            try {
                String userInfoStr = UserInfoPreferences.getString(USER, "");
                mUser = deSerialize(userInfoStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mUser;
    }

    /**
     * 清除用户信息
     *
     * @return
     */
    public static void clearUserInfo() {
        try {
            UserInfoPreferences.edit().putString(USER, serialize(new User())).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 验证用户是否已登录
     *
     * @author
     * @since 2015-11-19 下午04:57:36
     */
    public static boolean isUserLogin() {
        boolean isLogin = false;
        User userInfo = Config.getUserInfo();
        if (userInfo.getToken() != null && userInfo.getToken().length() > 0) {
            isLogin = true;
        }
        return isLogin;
    }

    /**
     * 序列化对象
     *
     * @return
     * @throws IOException
     */
    public static String serialize(User user) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(user);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static User deSerialize(String str) throws IOException, ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        User person = (User) objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return person;
    }

    /**
     * 序列化对象
     *
     * @return
     * @throws IOException
     */
    public static String serialize2(Object obj) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(obj);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deSerialize2(String str) throws IOException, ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        Object obj = (Object) objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return obj;
    }
    /**
     * SharedPreferences IP地址
     */
    public static final String IPADRESS = "IP_Address";
    public static SharedPreferences IpInfoPreferences = MApplication.getInstance().getSharedPreferences(IPADRESS, 0);
    public static String getIpAdress() {
        return IpInfoPreferences.getString(IPADRESS, "");
    }

    public static void setIpAdress(String ip) {
        IpInfoPreferences.edit().putString(IPADRESS, ip).commit();
    }

}
